﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RaProWorld
{
    class Ltan_player
    {
        Texture2D sprite;
        public float speed;
        float jumpSpeed;
        float jumpBeschl;
        public Vector2 position;
        public bool scrolling;
        bool keyLockLeft;
        bool keyLockRight;
        public bool dead;
        Vector2 movement;
        KeyboardState keyboardState = Keyboard.GetState();
        KeyboardState previousKeyboardState;
        int jumpingMode;
        int wackelRateCoolDown;
        public int wackelRate;
        public Rectangle hitbox;
        SoundEffect jumpSound;

        public Ltan_player(Texture2D newSprite,SoundEffect newJump) 
        {
            sprite = newSprite;
            speed = 0.0f;
            movement = new Vector2(0.0f, 0.0f);
            position = new Vector2(300.0f, (float)675 - (sprite.Height/2));
            scrolling = false;
            keyLockLeft = false;
            keyLockRight = false;
            dead = false;
            jumpingMode = 0;
            jumpSpeed = 0.0f;
            jumpBeschl = 0.0f;
            hitbox = new Rectangle((int)position.X-(sprite.Width/2), (int)position.Y-(sprite.Height/2), sprite.Width, sprite.Height);
            wackelRateCoolDown = 50;
            wackelRate = 0;
            jumpSound = newJump;
        }

        public void update(GameTime gameTime) 
        {
            hitbox.X = (int)position.X-(sprite.Width/2);
            hitbox.Y = (int)position.Y-(sprite.Height/2);
            wackel();
        }

        public void handleInput() 
        {
            previousKeyboardState = keyboardState;
            keyboardState = Keyboard.GetState();
            if (!dead)
            {
                if (keyboardState.IsKeyDown(Keys.Right))
                {
                    walkRight();
                }
                else if (keyboardState.IsKeyDown(Keys.Left))
                {
                    walkLeft();
                }
                else
                {
                    slowDown();
                }


                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    jump();
                    
                }
                else
                {
                    fall();
                }
            }
            else 
            {
                fall();
                scrolling = false;
            }

        }

        private void jump() 
        {
            if (jumpingMode == 0)
            {
                jumpBeschl = 0.0f;
                jumpingMode = 1;
                jumpSpeed = 0.0f;
                jumpSound.Play();
            }
            if (jumpingMode == 1)
            {
                jumpBeschl = jumpBeschl + 0.05f;
                if (jumpBeschl > Math.PI * 0.5f)
                {
                    jumpBeschl = (float)Math.PI * 0.5f;
                    jumpingMode = 2;
                }
                position.Y = 675.0f - (sprite.Height / 2) - ((float)Math.Sin(jumpBeschl) * 300.0f);
            }

            if (jumpingMode == 2)
            {
                jumpSpeed = jumpSpeed + 0.5f;
                if (jumpSpeed > 15.0f)
                {
                    jumpSpeed = 15.0f;
                }
                position.Y = position.Y + jumpSpeed;
                if (position.Y > 675.0f - (sprite.Height / 2))
                {
                    position.Y = 675.0f - (sprite.Height / 2);
                    jumpingMode = 0;
                }
            }
        }
        private void fall() 
        {
            if (jumpingMode == 1)
            {
                jumpingMode = 2;
            }

            if (jumpingMode == 2)
            {
                jumpSpeed = jumpSpeed + 0.5f;
                if (jumpSpeed > 15.0f)
                {
                    jumpSpeed = 15.0f;
                }
                position.Y = position.Y + jumpSpeed;
                if (position.Y > 675.0f - (sprite.Height / 2))
                {
                    position.Y = 675.0f - (sprite.Height / 2);
                    jumpingMode = 0;
                }
            }
        }

        private void walkLeft() 
        {
            scrolling = false;

            if (speed > 0)
            {
                speed = speed * -1.0f;
            }
            speed = speed - 0.2f;

            if (speed <= -5.0f)
            {
                speed = -5.0f;
            }

            position.X = position.X + speed;
            if (position.X < 0)
            {
                position.X = 0.0f;
            }
        }

        private void walkRight() 
        {
            if (speed < 0)
            {
                speed = speed * -1.0f;
            }
            speed = speed + 0.2f;
            if (speed >= 5.0)
            {
                speed = 5.0f;
            }
            position.X = position.X + speed;
            if (position.X > 600)
            {
                position.X = 600.0f;
                scrolling = true;
            }
            else
            {
                scrolling = false;
            }
        }

        private void slowDown() 
        {
            if (speed > 0.4f)
            {
                speed = speed - 0.5f;
            }
            else if (speed < -0.4f)
            {
                speed = speed + 0.5f;
            }
            else
            {
                speed = 0.0f;
            }
        }
        private void wackel()
        {
            if (wackelRateCoolDown == 0 || wackelRateCoolDown == 3 || wackelRateCoolDown == 6 || wackelRateCoolDown == 9 || wackelRateCoolDown == 12 || wackelRateCoolDown == 15) 
            {
                if(!keyboardState.IsKeyDown(Keys.Right)){
                    if (keyLockRight&&(!keyLockLeft)) 
                    {
                        if(keyboardState.IsKeyDown(Keys.Left))
                        {
                            wackelRate++;
                        }
                    }
                }
                if(!keyboardState.IsKeyDown(Keys.Left)){
                    if ((!keyLockRight) && keyLockLeft) 
                    {
                        if (keyboardState.IsKeyDown(Keys.Right)) 
                        {
                            wackelRate++;
                        }
                    }
                }
                if (keyboardState.IsKeyDown(Keys.Right))
                {
                    keyLockRight = true;
                }
                else 
                {
                    keyLockRight = false;
                }
                if (keyboardState.IsKeyDown(Keys.Left))
                {
                    keyLockLeft = true;
                }
                else 
                {
                    keyLockLeft = false;
                }

                
            }
            if (wackelRateCoolDown == 0) 
            {
                wackelRate = (int)(wackelRate*0.7f);
                wackelRateCoolDown = 18;
            }
            wackelRateCoolDown--;
        }

        public void reset() 
        {
            position = new Vector2(300.0f, (float)675 - (sprite.Height / 2));
            dead = false;
        }

        public void draw(SpriteBatch spriteBatch, GameTime gameTime) 
        {
            if (!dead)
            {
                spriteBatch.Draw(sprite,
                    position,
                    null,
                    Color.White,
                    0.0f,
                    new Vector2(sprite.Width / 2, sprite.Height / 2),
                    1.0f,
                    SpriteEffects.None,
                    0);
            }
            else 
            {
                spriteBatch.Draw(sprite,
                    position,
                    null,
                    Color.White,
                    90.0f,
                    new Vector2(sprite.Width / 2, sprite.Height / 2),
                    1.0f,
                    SpriteEffects.None,
                    0);
            }
        }
    }
}
